/**
 * ZHU framework. Copyright 2012-, all rights reserved.
 *
 * $Id: ConversationalController.java 39 2012-09-12 08:50:12Z zhuxiaopeng $
 * $Revision: 39 $
 * $Date: 2012-09-12 16:50:12 +0800 (三, 2012-09-12) $
 * $Author: zhuxiaopeng $
 */
package zhu.framework.web.jsf;

import javax.annotation.*;
import javax.enterprise.context.*;
import javax.faces.event.*;
import javax.inject.*;

/**
 * <p>
 * </p>
 * 
 * @author $Author: zhuxiaopeng $
 * @version $Revision: 39 $ - $Date: 2012-09-12 16:50:12 +0800 (三, 2012-09-12) $
 */
public abstract class ConversationalController {

	@PostConstruct
	public void initialize() {
		beginConversation();
	}

	public void initialize(final ComponentSystemEvent event) {
		beginConversation();
	}

	public void finish() {
		endConversation();
	}

	protected void beginConversation() {
		if (conversation.isTransient()) {
			conversation.begin();
		}
	}

	protected void endConversation() {
		if (conversation.isTransient() == false) {
			conversation.end();
			return;
		}
		throw new IllegalStateException();
	}

	@Inject
	private Conversation conversation;

}
